// pages/add/add.js
var api = require('./../../../config/api.js');
var app = getApp();
var util = require('./../../../utils/util.js');
const requests = require('../../../config/request');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        region: ['北京市', '北京市', '东城区'],
        switch1Checked: false,
        form : {
            username: "",
            cellphone: "",
            region_id1: "110000",
            region_id2: "110100",
            region_id3: "110101",
            address: "",
            is_default: 0,
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.address_id != 'undefined') {
            this.getAddressDetail(options.address_id)
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    bindRegionChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail)
        this.setData({
            region: e.detail.value
        })
        this.setData({
            'form.region_id1' : e.detail.code[0],
            'form.region_id2' : e.detail.code[1],
            'form.region_id3' : e.detail.code[2],
        })
    },

    switchChange: function (e) {
        if (e.detail.value) {
            this.setData({
                'form.is_default' : 1
            })
        } else {
            this.setData({
                'form.is_default' : 0
            })
        }
    },
    iptUsername: function(e){
        this.setData({
            'form.username': e.detail.value
        })
    },
    iptCellphone: function(e){
        this.setData({
            'form.cellphone': e.detail.value
        })
    },
    iptAddress: function(e){
        this.setData({
            'form.address': e.detail.value
        })
    },
    btnSubmit: function() {
        if (this.data.form.username == '') {
            wx.showToast({
              title: '请填写收货人',
              icon: 'none'
            })
            return;
        }
        if (this.data.form.cellphone == '' || this.data.form.cellphone.length != 11) {
            wx.showToast({
              title: '请填写正确的联系电话',
              icon: 'none'
            })
            return;
        }
        if (this.data.form.address == '') {
            wx.showToast({
              title: '请填写详细地址',
              icon: 'none'
            })
            return;
        }
        requests.addressEdit(this.data.form).then(({ code }) => {
            wx.navigateBack({
              delta: 1,
            })
        })
    },
    getAddressDetail: function (address_id) {
        requests.addressDetail({address_id:address_id})
            .then(({ detail }) => {
                detail.address_id = detail.id
                delete detail.id
                this.setData({
                    region:[detail.region_id1_name, detail.region_id2_name, detail.region_id3_name],
                    form: detail,
                    switch1Checked: detail.is_default == 1 ? true : false,
                })
        })
    }
})